<template>
  <div class="login-container center-flex">
    <div class="login-wrap">
      <img class="logo" src="/static/logo.png">

      <button class="center-flex btn wx-btn" hover-class="opacity" type="primary" open-type="getPhoneNumber" @getphonenumber="getphonenumber">
        <img src="/static/wechat.png" class="wx-icon">
        <div class="wx-text">微信手机号登陆</div>
      </button>
      <button @click="toBind" hover-class="opacity" class="center-flex btn phone-btn">手机验证码登陆</button>
    </div>
  </div>
</template>

<script>
import { login, phoneLogin } from '@/utils/login'
import { redirectBack, getCurrentOptions } from '@/utils'
import authority from '@/utils/authority'

export default {
  methods: {
    login() {
      authority.clear()
      login()
    },
    toBind() {
      const { back } = getCurrentOptions()
      wx.navigateTo({
        url: `/pages/bind/main?back=${back}`
      })
    },
    getphonenumber(e) {
      const { encryptedData, iv } = e.target
      if (!encryptedData) {
        return this.toBind()
      } else {
        phoneLogin({
          encrypted_data: encryptedData,
          iv
        }).then(() => {
          wx.showToast({
            title: '登陆成功'
          })
          redirectBack()
        }).catch((e) => {
          console.error(e)
          this.toBind()
        })
      }
    }
  },
  onShow() {
    this.login()
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  flex-direction: column;
}
.login-wrap {
  width: 100%;
  height: 778rpx;
  display: flex;
  align-items: center;
  flex-direction: column;
}
.logo,
.user-wrap {
  width: 310rpx;
  height: 100rpx;
  padding-bottom: 250rpx;
  text-align: center;
}
.btn {
  width: 650rpx;
  border: 2rpx solid #ff1d41;
  height: 86rpx;
  line-height: 1;
  border-radius: 43rpx !important;
  box-sizing: border-box;
  font-size: 32rpx;
  margin-bottom: 40rpx;
  &.opacity {
    opacity: 0.6;
  }
}
.wx-btn {
  background-image: linear-gradient(
    -180deg,
    rgb(254, 28, 68) 0%,
    rgb(244, 19, 111) 100%
  );
  .wx-text {
    color: white;
  }
}
.phone-btn {
  background: white;
  color: #ff1d41;
  border: 2rpx solid #ff1d41;
}
.bind-btn {
  margin-top: 80rpx;
}
.wx-icon {
  width: 50rpx;
  height: 43rpx;
  padding-right: 14rpx;
}
.avatar-wrap {
  width: 150rpx;
  height: 150rpx;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 30rpx;
  display: block;
  margin: 0 auto;
  .avatar {
    width: 150rpx;
    height: 150rpx;
  }
}
.nickname-wrap {
  padding-top: 30rpx;
  font-size: 30rpx;
}
</style>
